package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ReggieContext;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
       // 先把userId给塞进去
        shoppingCart.setUserId(ReggieContext.get());
       // 判断购物车里是否有记录
        // selece * from shopping_cart where dish_id= ? and setmeal_id = ?
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//         等于下面的
//        if (shoppingCart.getDishId()!=null){
//            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//        }else {
//            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        }

        ShoppingCart  dbCart = this.getOne(lqw);
        // 判断dbCart是否为空 如果为空,说明是第一次添加
        if (dbCart==null){
            // save
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            return shoppingCart;
        }else {
            // 如果不为空说明已经有数据了 只需要修改就可以了
            dbCart.setNumber(dbCart.getNumber()+1);
            this.updateById(dbCart);
            return dbCart;
        }

    }

    // 购物车数量减1
    @Override
    public void sub(ShoppingCart shoppingCart) {
        // todo 购物车数量减1

        //  count = update shopping_cart set number-1 where dishId= ?
        // if(count = 0 ){   删除  }
        // delete from shopping_cart where number=0 ;
       // ShoppingCart 库
       // number-- ;

        // number - 1

        if (shoppingCart.getDishId()!=null){
            ShoppingCart sc =this.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getDishId, shoppingCart.getDishId()));
            Integer number = sc.getNumber();
            // 减1
            LambdaUpdateWrapper<ShoppingCart> luw = new LambdaUpdateWrapper<>();
            luw.set(ShoppingCart::getNumber,number-1);
            luw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
            this.update(luw);
            if (number==1){
                LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
                this.remove(lqw);
            }
        }else {
            ShoppingCart sc = this.getOne(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId()));
            LambdaUpdateWrapper<ShoppingCart> luw = new LambdaUpdateWrapper<>();
            Integer number = sc.getNumber();
            luw.set(ShoppingCart::getNumber,number-1);
            luw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            this.update(luw);
            if (number==1){
                LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
                this.remove(lqw);
            }
        }
    }
}
